package com.lb.shopguide.base;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.jaeger.library.StatusBarUtil;
import com.lb.shopguide.event.guide.BackToHomeEvent;
import com.lb.shopguide.ui.activity.BossMainActivity;
import com.lb.shopguide.ui.activity.GuideMainActivity;
import com.lb.shopguide.ui.fragment.boss.FragmentDataOverview;
import com.lb.shopguide.ui.fragment.boss.FragmentMerchant;
import com.lb.shopguide.ui.fragment.boss.FragmentShopData;
import com.lb.shopguide.ui.fragment.guide.FragmentGuideMine;
import com.lb.shopguide.ui.fragment.guide.FragmentHome;
import com.tendcloud.tenddata.TCAgent;

import org.greenrobot.eventbus.EventBus;


/**
 * 懒加载
 * Created by YoKeyword on 16/6/5.
 */
public abstract class BaseMainFragment extends BaseFragment{


    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        initData();
        initView();
    }

    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        TCAgent.onPageStart(_mActivity, getPageTag());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            if (this instanceof FragmentDataOverview || this instanceof FragmentShopData || this instanceof FragmentMerchant
                    || this instanceof FragmentHome || this instanceof FragmentGuideMine) {
                StatusBarUtil.setColor(_mActivity, Color.rgb(253, 91, 89), 0);
                StatusBarUtil.setDarkMode(_mActivity);
            } else {
                StatusBarUtil.setColor(_mActivity, Color.rgb(255, 255, 255), 0);
                StatusBarUtil.setLightMode(_mActivity);
            }
        }
    }

    @Override
    public void onSupportInvisible() {
        super.onSupportInvisible();
        TCAgent.onPageEnd(_mActivity, getPageTag());
    }

    /**
     * 处理回退事件
     *
     * @return
     */
    @Override
    public boolean onBackPressedSupport() {
        if (getChildFragmentManager().getBackStackEntryCount() > 1) {
            popChild();
        } else {
            if (this instanceof FragmentHome) {   // 如果是 第一个Fragment 则退出app
                ((GuideMainActivity) _mActivity).onBackPressedSupport();
            } else if (this instanceof FragmentDataOverview || this instanceof FragmentShopData) {
                ((BossMainActivity) _mActivity).onBackPressedSupport();
            } else {
                EventBus.getDefault().post(new BackToHomeEvent());
            }
        }
        return true;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

}
